package com.pi.pro.xcmg.am.was.fac.handler;

import com.pi.framework.core.util.StringUtils;
import com.pi.wms.common.WmsTextUtils;
import com.pi.wms.entity.TwaDocSoHeader;
import com.pi.wms.entity.TwaDocWaveHeader;
import com.pi.wms.fac.CustomPredicateQuery;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *
 */
public class WaveCustomPredicate extends CustomPredicateQuery<TwaDocWaveHeader> {

    @Override
    public void toPredicate(Root<TwaDocWaveHeader> root, List<Predicate> predicates, CriteriaQuery<?> criteriaQuery, CriteriaBuilder builder, Map<String, ?> query) {
        if (!this.validate(root, predicates, criteriaQuery, builder, query)){
            return;
        }

        Date date = null;
        String fromdate = (String)query.get("fromdate");
        if(!StringUtils.isNullOrBlank(fromdate)) {
            fromdate = fromdate.replace("-", "");
            date = WmsTextUtils.plainDate(fromdate, true);
            if(date != null) {
                predicates.add(builder.greaterThanOrEqualTo(root.get("createdDate"), date));
            }
        }

        String todate = (String)query.get("todate");
        if(!StringUtils.isNullOrBlank(todate)) {
            todate = todate.replace("-", "");
            date = WmsTextUtils.plainDate(todate, false);
            if(date != null) {
                predicates.add(builder.lessThanOrEqualTo(root.get("createdDate"), date));
            }
        }
    }
}
